Brian Kernighan算法

Posted by Liao on 2023-01-11

Brian Kernighan是一种位移运算的算法,将n和n-1按位与后,n最右边的1会被清除。故用于清除二进制串中最右边的1。

1
n & (n - 1)